package com.moxigua.iotbox.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.widget.Toast;

import com.xiasuhuei321.loadingdialog.view.LoadingDialog;
import com.zhy.autolayout.AutoLayoutActivity;

import butterknife.ButterKnife;

/**
 * Created by junerver on 2017/5/10.
 */

public abstract class BaseActivity extends AutoLayoutActivity {

    protected static final String TAG = "BaseActivity";
    private Toast mToast;
    protected Context mContext;
    protected LoadingDialog mLoadingDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int layout = getLayoutId();
        if (layout == 0) {
            throw new IllegalStateException("Please specify root layout resource id for " + getClass().getSimpleName());
        } else {
            setContentView(layout);
            mContext = this;
            ButterKnife.bind(this);
            mToast = Toast.makeText(mContext, "", Toast.LENGTH_SHORT);

            initView();
        }
    }

    protected void showLoadingDialog() {
        mLoadingDialog = new LoadingDialog(this);
        mLoadingDialog.setLoadingText("请稍等")
                .setSuccessText("成功")
                .setFailedText("失败")
                .closeSuccessAnim()
                .closeFailedAnim()
                .setLoadSpeed(LoadingDialog.Speed.SPEED_TWO)
                .show();
    }

    abstract protected void initView();

    abstract protected int getLayoutId();

    protected void showShortToast(@StringRes int resId) {
        mToast.setText(resId);
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.show();
    }

    protected void showShortToast(String text) {
        mToast.setText(text);
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.show();
    }

    protected void showLongToast(@StringRes int resId) {
        mToast.setText(resId);
        mToast.setDuration(Toast.LENGTH_LONG);
        mToast.show();
    }

    protected void showLongToast(String text) {
        mToast.setText(text);
        mToast.setDuration(Toast.LENGTH_LONG);
        mToast.show();
    }

    protected void startActivity(Class<?> cls, Bundle... bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null && bundle.length > 0) {
            intent.putExtras(bundle[0]);
        }
        startActivity(intent);
    }

}
